﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ERP.Workflow.Designer.BlazorUI.Helper
{
    public class EnumHelper
    {
        public static IEnumerable<(object Value, string Label)> GetValueLabelList(Type type)
        {
            List<object> list = new List<object>();
            foreach (var rs in Enum.GetValues(type))
            {
                list.Add((int)rs);
            }
            var _nameList = Enum.GetNames(type).Cast<int>;

            var _valueLabelList = list.Select(value => (value, GetDisplayName(type, value)));
            return _valueLabelList;

        }
        public static string GetDisplayName(Type type, object value)
        {
            var enumName = Enum.GetName(type, value);
            var fieldInfo = type.GetField(enumName??"");
            return fieldInfo!.GetCustomAttribute<DescriptionAttribute>(true)?.Description ?? enumName;
        }
    }

}
